package edu.ucsb.cs56.W12.chatclient.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import edu.ucsb.cs56.W12.chatclient.controller.ClientController;

/**
 * ClientWindow class represent a JFrame window which has components that are needed for chatting
 * @author Peng Wang and Andro Stotts
 * @version 0.1
 */
public class ClientWindow extends JFrame{

	private static final long serialVersionUID = 1L;
	private JTextField tfInput;
	private TextArea taOutput;
	private JTextField tfUsername;
	private JTextField tfServerIp;
	private JList listContacts;
	private JLabel lblContact;
	private JLabel lblUserName;
	private JLabel lblServerIp;
	private JButton btConnect;
	private static ClientWindow window;
	private ClientController controller;
	private String name;
	private String ip;
	
	/**
	 * Default Constructor
	 */
	private ClientWindow(){
		controller = ClientController.getController();
		tfInput = new JTextField();
		taOutput = new TextArea();
		listContacts = new JList(controller.getContacts());
		lblContact = new JLabel("Contacts");
		lblUserName = new JLabel("Username: ");
		lblServerIp = new JLabel("Server IP: ");
		tfUsername = new JTextField(20);
		tfServerIp = new JTextField(20);
		//default connection
		tfServerIp.setText("192.168.1.3");
		btConnect = new JButton("Connect to Server");
		name = "";
		ip = "";
	}
	
	/**
	 * get the instance of the ClientWindow object
	 * @return instance of the ClientWindow
	 */
	public static ClientWindow getWindow(){
		if(window == null){
			window = new ClientWindow();
		}
		return window;
	}
	
	/**
	 * create the login window
	 */
	public void launchLoginWindow(){
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(100, 100);
		this.setSize(300, 200);
		this.setTitle("Chatting Client");
		this.setLayout(new FlowLayout());

		this.getContentPane().add(lblUserName);
		this.getContentPane().add(tfUsername);
		this.getContentPane().add(lblServerIp);
		this.getContentPane().add(tfServerIp);
		this.getContentPane().add(btConnect);
		btConnect.addActionListener(new MyButtonListener());
		this.setVisible(true);
	}
	
	/**
	 * create the chat window
	 */
	public void launchChatWindow(){

		this.getContentPane().removeAll();
		this.setLayout(new BorderLayout());
		this.setSize(600, 400);
		
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		rightPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		rightPanel.setLayout(new BorderLayout());
		rightPanel.add(taOutput, BorderLayout.CENTER);
		rightPanel.add(tfInput, BorderLayout.SOUTH);
		
		leftPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		leftPanel.setLayout(new BorderLayout());
		leftPanel.add(lblContact, BorderLayout.NORTH);
		leftPanel.add(listContacts, BorderLayout.CENTER);
		
		taOutput.setEditable(false);
		this.getContentPane().add(leftPanel, BorderLayout.WEST);
		this.getContentPane().add(rightPanel, BorderLayout.CENTER);
		this.repaint();
		tfInput.addActionListener(new InputListener());
	}
	
	/**
	 * getTaOutput returns the TextArea component that displays the messages
	 * @return the TextArea component that displays the messages
	 */
	public TextArea getTaOutput(){
		return taOutput;
	}
	
	/**
	 * MybuttonListener implements ActionListener, it's used to handle
	 * actions when button is clicked
	 * @author Peng Wang and Andro Stotts
	 * @version 0.1
	 */
	class MyButtonListener implements ActionListener{
		private ClientWindow window = ClientWindow.getWindow();
		/**
		 * actionPerformed is used to handle an ActionEvent
		 * @param e e is the action event
		 */
		public void actionPerformed(ActionEvent e) {
			name = tfUsername.getText();
			ip = tfServerIp.getText();
			window.launchChatWindow();
			controller.connectServer(ip, name);		
		}	
	}
	
	/**
	 * InputListener implements ActionListener, it is used for handling the action when user
	 * click on the enter on keyboard
	 * @author Peng Wang and Andro Stotts
	 * @version 0.1
	 */
	class InputListener implements ActionListener{
		/**
		 * actionPerformed is used to handle an ActionEvent
		 * @param e e is the action event
		 */
		public void actionPerformed(ActionEvent e) {
			String text = tfInput.getText().trim();
			tfInput.setText("");
			controller.sendMsg2Server(text);
		}	
	}
}
